import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { streetManagementStateType } from './typings'
import { getStreetSelectListByUser } from '~/api/assetManagement/streetManagement/streetManagement'
import { filterValueEnum } from '~/utils/util'

const streetManagement: Module<streetManagementStateType, streetManagementStateType> = {
  namespaced: true,
  state: {
    streetSelectList: [],
    streetEnum: {}
  },
  getters: {
    streetSelectList: (state) => state.streetSelectList,
    streetEnum: (state) => state.streetEnum
  },
  mutations: {
    SET_WORKAREA_SELECT_LIST: (state, payload) => {
      state.streetSelectList = payload
      state.streetEnum = filterValueEnum(payload, 'id', 'name')
    }
  },
  actions: {
    // 获取所属街道下拉列表
    async GetStreetSelectListByUser<S, T> ({ commit }: ActionContext<S, T>, params: T) {
      return await getStreetSelectListByUser<T>(params).then((res: AxiosResponse) => {
        commit('SET_WORKAREA_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    }
  }
}

export default streetManagement
